<?php

namespace app\web\controller;

use app\common\controller\Web;
use app\common\model\AlgorithmCaseModel;
use app\common\model\AlgorithmItem;
use app\common\model\AlgorithmSector;
use app\common\model\Algorithm;
use app\common\model\AlgorithmOther;
use app\common\model\AlgorithmQuestionModel;
use app\common\model\AlgorithmSceneModel;
use app\common\model\Apply;
use app\common\model\CasesItemModel;
use app\common\model\Link;
use app\common\model\Company;
use app\common\model\Navigation;
use app\common\model\Product;
use app\common\model\ProductItem;
use app\common\model\Resource;
use app\common\model\ResourceTopic;
use app\common\model\ResourceTopicRel;
use app\common\model\ResourceType;

/**
 * 首页
 * User: wufeng
 * Date: 2020/06/24.
 */
class Index extends Web
{
	public function index()
	{
		$resLists = (new Resource())->where([])->select();
		foreach ($resLists as $list) {
			$list['pic'] = _sImage($list['pic']);
		}
		$this->assign('resLists', $resLists);
		//最新文章
		$newLists = (new Resource())->where([])->order('ctime desc')->limit(20)->select();
		$this->assign('newLists', $newLists);
		$linkLists = (new Link())->where([])->select();
		$this->assign('linkLists', $linkLists);
		return view('index');
	}

	public function lists()
	{
		$nav_id = $this->request->param('id', 1);
		$nav = (new Navigation())->get($nav_id);
		$this->assign('nav', $nav);
		$this->assign('nav_id', $nav_id);
		$resLists = (new Resource())->where(['nav_id' => $nav_id])->select();
		foreach ($resLists as $list) {
			$list['pic'] = _sImage($list['pic']);
			$list['ctime'] = getTime($list['ctime']);
		}
		$this->assign('resLists', $resLists);
		//最新文章
		$newLists = (new Resource())->where([])->order('ctime desc')->limit(20)->select();
		$this->assign('newLists', $newLists);
		return view('lists');
	}

	public function detail()
	{
		$res_id = $this->request->param('id', 1);
		$res = (new Resource())->get($res_id);
		$res['nav'] = (new Navigation())->get(['id' => $res['nav_id']])['title'];
		$res['ctime'] = getTime($res['ctime']);
		$this->assign('res', $res);
		//上一篇
		$front_id = $res_id - 1;
		if ($front_id == 0) {
			$front['id'] = $front_id;
			$front['title'] = '没有了';
		} else {
			$frontOne = (new Resource())->where([
													'nav_id' => $res['nav_id'],
													'id'     => $front_id
												])->find();
			if ($frontOne) {
				$front['id'] = $front_id;
				$front['title'] = $frontOne['title'];
			} else {
				$front['id'] = 0;
				$front['title'] = '没有了';
			}
		}
		//下一篇
		$rear_id = $res_id + 1;
		$rearOne = (new Resource())->where([
											   'nav_id' => $res['nav_id'],
											   'id'     => $rear_id
										   ])->find();
		if ($rearOne) {
			$rear['id'] = $rear_id;
			$rear['title'] = $rearOne['title'];
		} else {
			$rear['id'] = 0;
			$rear['title'] = '没有了';
		}
		$this->assign('front', $front);
		$this->assign('rear', $rear);
		//为您推荐
		$recommend = json_decode($res['recommend'], true);
		$recommendLists = (new Resource())->where(['id' => $recommend])->order('ctime desc')->select();
		foreach ($recommendLists as $list) {
			$list['pic'] = _sImage($list['pic']);
		}
		$this->assign('recLists', $recommendLists);
		//最新文章
		$newLists = (new Resource())->where([])->order('ctime desc')->limit(20)->select();
		$this->assign('newLists', $newLists);
		// 获取协议类型
		$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http';
		// 获取主机名(包括域名和端口)
		$host = $_SERVER['HTTP_HOST'];
		// 获取资源路径
		$uri = $_SERVER['REQUEST_URI'];
		// 拼接完整的URL
		$url = $protocol . '://' . $host . $uri;
		$this->assign('url', $url);
		return view('detail');
	}
}
